package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    //目的地mapper
    private DestinationMapper destinationMapper;
    @Reference
    private IDestinationService destinationService;


    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //设置游记状态
        qo.setState(Travel.STATE_RELEASE);
        //设置游记公开
        qo.setOvert(1);
        List<Travel> list = travelMapper.query4List(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Travel> selectAll() {
        return null;
    }

    @Override
    public Travel selectByPrimaryKey(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public AjaxResult saveOrUpdate(Travel travel, UserInfo userInfo) {
        //获取内容
        TravelContent content = travel.getTravelContent();
        Date date = new Date();
        AjaxResult result = new AjaxResult();
        //注入当前用户
        travel.setAuthor(userInfo);
        //设置summary
        travel.setSummary("AI识别中...");
        //设置最后修改时间
        travel.setLastUpdateTime(date);
        //模拟审核通过
        travel.setState(Travel.STATE_RELEASE);
        if (travel.getId() == null) {
            //保存操作
            //设置创建时间
            travel.setCreateTime(date);
            //设置审核时间
            travel.setReleaseTime(date);
            //保存
            travelMapper.insert(travel);
            //设置内容id
            content.setId(travel.getId());
            //保存内容对象
            travelContentMapper.insert(content);
        } else {
            //编辑操作
            travelMapper.updateByPrimaryKey(travel);
            content.setId(travel.getId());
            travelContentMapper.update(content);
        }

        return result.addData(travel.getId());
    }

    @Override
    public void updataState(Long id, int state) {
        travelMapper.updateState(id, state);
    }

    @Override
    public List<Travel> selectByDestIdTop3(Long id) {
        return travelMapper.selectByDestIdTop3(id);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public List<Travel> selectByAuthorId(Long AuthorId) {
        List<Travel> travels = travelMapper.selectByAuthorIdTop5(AuthorId);
        if (travels != null && travels.size() > 0) {
            for (Travel travel : travels) {
                TravelContent travelContent = travelContentMapper.selectByPrimaryKey(travel.getId());
                travel.setTravelContent(travelContent);
                //设置目的地的最终父目的地
                Destination dest = destinationService.get(travel.getDest().getId());
                travel.setDest(dest);
            }
        }
        return travels;
    }

    @Override
    public int query4CountByAuthorId(Long AuthorId) {
        return travelMapper.query4CountByAuthorId(AuthorId);
    }

    //查询全部游记
    public List<Travel> listAll() {
        return travelMapper.selectAll();
    }

    /**
     * 查询用户所有的游记
     *
     * @param qo
     * @param id
     * @return
     */
    public PageInfo queryByAuthorId(TravelQuery qo, Long id) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //设置游记状态
        qo.setState(Travel.STATE_RELEASE);
        //设置游记公开
        qo.setOvert(1);
        //查询用户的所有游记
        List<Travel> list = travelMapper.selectByAuthorId(id);
        if (list != null && list.size() > 0) {
            for (Travel travel : list) {
                //设置目的地的最终父目的地
                Destination dest = destinationService.get(travel.getDest().getId());
                travel.setDest(dest);

            }
        }
        return new PageInfo(list);
    }

    /**
     * 用户游记回复总数
     *
     * @param userId
     * @return
     */
    public Integer getReplyCountByAuthorId(Long userId) {
        Integer replyCount = travelMapper.selectReplyCountByAuthorId(userId);
        if (replyCount == null) {
            return 0;
        }
        return replyCount;
    }

    /**
     * 用户游记阅读总数
     *
     * @param userId
     * @return
     */
    public Integer getViewCountByAuthorId(Long userId) {
        Integer viewCount = travelMapper.selectViewCountByAuthorId(userId);
        if (viewCount == null) {
            return 0;
        }
        return viewCount;
    }

    /**
     * 用户游记收藏
     *
     * @param userId
     * @return
     */
    public List<Travel> getByUserId(Long userId) {
        return travelMapper.selectAllByUserId(userId);
    }

    /**
     * 删除用户游记的收藏
     *
     * @param id
     * @return true 成公删除
     */
    public boolean deleteCollection(Long id) {
        try {
            travelMapper.deleteRelationByTravelId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
